﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace WindowsFormsApplication1.Filtry
{
    class Delacja
    {
         public Bitmap bitmapFiltr { get; set; }
        private int MacierzWidthIHight,width,height;
        private Bitmapp bitmap ;
        Double[,] Macierz;

        public Delacja(int MacierzWidthIHight, int width, int height, Bitmapp bitmap, Double[,] Macierz)
        {
            this.width = width; this.height = height;
            this.bitmap = bitmap; this.MacierzWidthIHight = MacierzWidthIHight;
            this.Macierz=Macierz;
        }

        public Bitmap ZwrocObrazekPoDelacji()
        {
            this.bitmapFiltr = new Bitmap(this.width, this.height);
            Double wynikR = 255, wynikG = 255, wynikB = 255;
            int i1 , i2 ;
            int j1, j2;
            int xPixel , yPixel ;

            for (int i = 0; i < this.height; i++)
            {
                for (int j = 0; j < this.width; j++)
                {
                    if (j <= MacierzWidthIHight / 2 || j > this.width - MacierzWidthIHight / 2 - 1 || i <= MacierzWidthIHight / 2 || i > this.height - MacierzWidthIHight / 2 - 1)
                    {
                        bitmapFiltr.SetPixel(j, i, bitmap.GetPixel(j, i));
                    }
                    else
                    {
                         xPixel = j - MacierzWidthIHight / 2; yPixel = i - MacierzWidthIHight / 2;
                        
                        for (int ii = 0; ii < MacierzWidthIHight; ii++)
                        {
                            for (int jj = 0; jj < MacierzWidthIHight; jj++)
                            {
                                if( Macierz[ii,jj]==1)
                                {
                                    if (wynikB > Convert.ToDouble(bitmap.GetPixelB(xPixel, yPixel)))
                                    {
                                        wynikB = Convert.ToDouble(bitmap.GetPixelB(xPixel, yPixel));
                                    }
                                    if (wynikR > Convert.ToDouble(bitmap.GetPixelR(xPixel, yPixel)))
                                    {
                                        wynikR = Convert.ToDouble(bitmap.GetPixelR(xPixel, yPixel));
                                    }
                                    if (wynikG > Convert.ToDouble(bitmap.GetPixelG(xPixel, yPixel)))
                                    {
                                        wynikG = Convert.ToDouble(bitmap.GetPixelG(xPixel, yPixel));
                                    }
                                }
                                xPixel++; 
                            }
                            yPixel++; xPixel = j - MacierzWidthIHight / 2;

                        }
                        bitmapFiltr.SetPixel(j, i, Color.FromArgb((int)wynikR, (int)wynikG, (int)wynikB));
                        wynikR = 255; wynikG = 255; wynikB = 255;
                        }
                }
            }


            // a teraz krawedzie
             i1 = MacierzWidthIHight / 2 + 1; i2 = this.height - MacierzWidthIHight / 2 - 1;
             j1 = MacierzWidthIHight / 2 + 1; j2 = this.width - MacierzWidthIHight / 2 - 1;
            for (int i = 0; i < this.height; i++)
            {
                for (int j = 0; j < this.width; j++)
                {

                    if (i <= MacierzWidthIHight / 2)
                    {
                        bitmapFiltr.SetPixel(j, i, bitmapFiltr.GetPixel(j, i1));
                    }
                    if (i > this.height - MacierzWidthIHight / 2 - 1)
                    {
                        bitmapFiltr.SetPixel(j, i, bitmapFiltr.GetPixel(j, i2));
                    }
                    if (j <= MacierzWidthIHight / 2)
                    {
                        bitmapFiltr.SetPixel(j, i, bitmapFiltr.GetPixel(j1, i));
                    }
                    if (j > this.width - MacierzWidthIHight / 2 - 1)
                    {
                        bitmapFiltr.SetPixel(j, i, bitmapFiltr.GetPixel(j2, i));
                    }
                }
            }

            return this.bitmapFiltr;
        }
    }
}
